package com.chi.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chi.comm.Const;
import com.chi.pojo.Advise;
import com.chi.service.AdviseService;
import com.opensymphony.xwork2.ModelDriven;

import jdk.nashorn.internal.ir.RuntimeNode.Request;
@Component("adviseAction")
public class AdviseAction implements ModelDriven<Advise>,RequestAware {
    @Autowired
	private AdviseService adviseservice;
    private int pageNo;
	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	private String option;

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}
	private Map<String,Object> requestmap;
	Advise advise =new Advise();
	@Override
	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		requestmap =arg0;
	}

	public String save(){
		adviseservice.saveAd(advise);
		return "success"; 
		}
	public String delete(){
		adviseservice.deleteAd(advise);
		return "success";
	}
	public String findById()
	{
		String msg = Const.ERROR;
		Advise advi1 = adviseservice.findById(advise.getId());
		if(advi1 != null)
		{
			if (Const.UPDATE.equalsIgnoreCase(option)) {
				requestmap.put("advise", advi1);
				msg = Const.UPDATE;// update -->dispatcher WEB-INF/views/emp_update.jsp
			}else{
			List<Advise> adviList = new ArrayList<>();
			adviList.add(advi1);
			requestmap.put("adviList", adviList);
			msg = Const.SUCCESS;
			}
		}
		
		return msg;
	}
	public String findAll()
	{		
		 pageNo = 1;
		 
		String msg = Const.ERROR;
			List<Advise> adviList = adviseservice.findAll(pageNo);
			System.out.println(adviList);
			if (adviList != null && adviList.size() > 0) {
				requestmap.put("adviList", adviList);
				msg = Const.SUCCESS;
		}
		return msg;
	}
	//分页
	
	public String page()
	{	
		
		
		
		String msg = Const.ERROR;
		
		List<Advise> advis = adviseservice.getPage(pageNo).getList();
		int a = adviseservice.getPage(pageNo).getPageNo();
		System.out.println(a);
		System.out.println(advis);
		if(advis!= null && advis.size() > 0)
		{
			requestmap.put("adviList", advis);
			requestmap.put("pageNo", a);
			msg = Const.SUCCESS; 
		}
		return msg;
	}
	@Override
	public Advise getModel() {
		// TODO Auto-generated method stub
		
		return advise;
	}

}
